jsonpath

您所在的位置:网站首页 json path tester jsonpath

jsonpath

#jsonpath| 来源: 网络整理| 查看: 265

Build Status

jsonpath

Query JavaScript objects with JSONPath expressions. Robust / safe JSONPath engine for Node.js.

Query Example var cities = [ { name: "London", "population": 8615246 }, { name: "Berlin", "population": 3517424 }, { name: "Madrid", "population": 3165235 }, { name: "Rome", "population": 2870528 } ]; var jp = require('jsonpath'); var names = jp.query(cities, '$..name'); // [ "London", "Berlin", "Madrid", "Rome" ] Install

Install from npm:

$ npm install jsonpath JSONPath Syntax

Here are syntax and examples adapted from Stefan Goessner's original post introducing JSONPath in 2007.

JSONPath Description $ The root object/element @ The current object/element . Child member operator .. Recursive descendant operator; JSONPath borrows this syntax from E4X * Wildcard matching all objects/elements regardless their names [] Subscript operator [,] Union operator for alternate names or array indices as a set [start:end:step] Array slice operator borrowed from ES4 / Python ?() Applies a filter (script) expression via static evaluation () Script expression via static evaluation

Given this sample data set, see example expressions below:

{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } } }

Example JSONPath expressions:

JSONPath Description $.store.book[*].author The authors of all books in the store $..author All authors $.store.* All things in store, which are some books and a red bicycle $.store..price The price of everything in the store $..book[2] The third book $..book[(@.length-1)] The last book via script subscript $..book[-1:] The last book via slice $..book[0,1] The first two books via subscript union $..book[:2] The first two books via subscript array slice $..book[?(@.isbn)] Filter all books with isbn number $..book[?(@.price $['store']), and in other cases, can be just plain wrong (e.g. [ => $).

Other Minor Differences

As a result of using a real parser and static evaluation, there are some arguable bugs in the original library that have not been carried through here:

strings in subscripts may now be double-quoted final step arguments in slice operators may now be negative script expressions may now contain . and @ characters not referring to instance variables subscripts no longer act as character slices on string elements non-ascii non-word characters are no-longer valid in member identifier names; use quoted subscript strings instead (e.g., $['$'] instead of $.$) unions now yield real unions with no duplicates rather than concatenated results License

MIT



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3